import re
import random
from dataclasses import dataclass

from django.shortcuts import render
from pycparser.ply.yacc import token
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from user.models import UserModel
from user.ser import UserSer
from utils.myjwt import jwtweb
from utils.mymessage import send_message
from utils.myredis import redistoll


# Create your views here.
#密码登录与注册
class LoginView(APIView):
    def post(self, request):
        data = {}
        account=request.data.get('account')
        password=request.data.get('password')
        if re.match(r'^1[3-9]\d{9}$',account):
            userdata=UserModel.objects.filter(phone=account).first()
            data["phone"]=account
        elif re.match(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9.]+$',account):
            userdata=UserModel.objects.filter(email=account).first()
            data["email"] = account
        else:
            userdata=UserModel.objects.filter(username=account).first()
            data["username"] = account
        if userdata:
            if userdata.password==password:
                token=jwtweb.gettoken(userdata.id,account)
                return Response({"msg":"登录成功！！",'token':token,"id":userdata.id,"code":200})
            else:
                return Response({"msg":"密码错误，登录失败！！！！","code":400})
        else:
            ser=UserSer(data=data)
            if ser.is_valid():
                ser.save()
                return Response({"msg":"注册成功！！","code":200})
            else:
                return Response({"msg":"账号或密码不规则！！","code":402},ser.errors)

#发送验证码
class SendCodeView(APIView):
    def post(self, request):
        phone=request.data.get('phone')
        code=redistoll.getstr(phone)
        return Response({"msg":"ok","data":code})
    # def post(self, request):
        # phone=request.data.get('phone')
        # if re.match(r'^1[3-9]\d{9}$',phone):
        #     code=str(random.randint(10000,99999))
        #     if send_message(phone,code):
        #         redistoll.setex(phone,code,60)
        #         return Response({"msg":"发送成功！！！","code":200})
        #     else:
        #         return Response({"msg":"发送失败！！","code":400})
        # else:
        #     return Response({"msg":"手机号不正确","code":400})

#验证码登录
class LoginCodeView(APIView):
    def post(self, request):
        phone=request.data.get('phone')
        code=str(request.data.get('code'))
        if re.match(r'^1[3-9]\d{9}$',phone):
            userdata=UserModel.objects.filter(phone=phone).first()
            if userdata:
                if code==redistoll.getstr(phone):
                    token=jwtweb.gettoken(userdata.id,phone)
                    return Response({"msg":"登录成功！！！","token":token,"code":200,"id":userdata.id})
                else:
                    return Response({"msg":"验证码错误，登录失败！！！","code":400})
            else:
                return Response({"msg":"用户不存在"})
        else:
            return Response({"msg":"手机号不符合规则！！","code":404})

#查询用户
class FindUserView(APIView):
    def get(self,request):
        id=request.query_params.get('id')
        userdata=UserModel.objects.filter(id=id).first()
        ser=UserSer(userdata)
        return Response({"msg":"查询成功","data":ser.data,"code":200})
